package org.chuan.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.chuan.base.annotation.SelfPermission;
import org.chuan.base.utils.AjaxResult;
import org.chuan.base.utils.PageList;
import org.chuan.org.domain.Department;
import org.chuan.org.query.DepartmentQuery;
import org.chuan.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dep")
@Api(value = "部门的相关文档",description = "部门相关的CRUD")
//@CrossOrigin
@SelfPermission(name = "部门管理")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @ApiOperation(value = "查询全部")
    @SelfPermission(name = "查询所有部门")
    public List<Department> queryAll(){
        try {
            return departmentService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询")
    @SelfPermission(name = "查询一个部门")
    public Department queryById(@PathVariable("id") Long id){
        try {
            return departmentService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除")
    @SelfPermission(name = "部门删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("操作失败："+e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "新增和修改")
    @SelfPermission(name = "部门的增加或修改")
    public AjaxResult saveOrUpdate(@RequestBody @ApiParam(value = "id为空为新增，否则为修改") Department department){
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("操作失败："+e.getMessage());
        }
    }

    @PostMapping
    @SelfPermission(name = "部门分页查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        try {
            return departmentService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/tree")
    @SelfPermission(name = "部门树形结构")
    public List<Department> queryDeptTree(){
        try {
            return departmentService.queryDeptTree();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
